#include <QPalette>
#include <QPainter>
#include <QMouseEvent>
#include <QFileDialog>
#include "musicListControl.h"
#include "globalValue.h"

musicListControl::musicListControl(QWidget *parent) : QWidget(parent)
{

    this->setFixedHeight(30);
    this->setMinimumWidth(50);


    wherePointing = -1;
    this->setMouseTracking(true);

}

void musicListControl::paintEvent(QPaintEvent *event)
{
    QPen pen;
    QBrush brush;
    QColor color;

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    brush.setStyle(Qt::SolidPattern);

    //color.setRgb();
    painter.fillRect(this->rect(), widgetBack);


    if(wherePointing == 1)
    {

        color.setRgb(0x00, 0x00, 0xFF, 0x88);
        brush.setColor(color);
        pen.setColor(color);
        painter.setPen(pen);
        painter.setBrush(brush);
        painter.drawEllipse(QPointF(15, 15), 14, 14);

    }

    color.setRgb(0xFF, 0xFF, 0xFF, 0xAA);
    pen.setColor(color);
    pen.setWidth(2);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pen);

    painter.drawLine(QPointF(15, 3), QPointF(15, 27));
    painter.drawLine(QPointF(3, 15), QPointF(27, 15));
}

void musicListControl::mouseMoveEvent(QMouseEvent *event)
{
    if(event->x() >= 3 && event->x() <= 27
            && event->y() >= 3 && event->y() <= 27)
    {
        wherePointing = 1;
        this->setCursor(Qt::PointingHandCursor);
    }
    else
    {
        wherePointing = -1;
        this->setCursor(Qt::ArrowCursor);
    }
    update();
}

void musicListControl::mousePressEvent(QMouseEvent *event)
{
    if(event->x() >= 3 && event->x() <= 27
            && event->y() >= 3 && event->y() <= 27)
    {

        QUrl fileName = QFileDialog::getOpenFileUrl(this,
                                                    "选择音频文件",
                                                    QUrl("./"));
        printf("path: %s \nfileName: %s\n",
               fileName.fileName().toUtf8().data(),
               fileName.path().toUtf8().data());
        if(!fileName.isEmpty())
            emit musicAddnew(fileName);
    }


}
